#include "cropping.h"
#include "drawingarea.h"
using namespace std;

//************************************************************* Constructor

Cropping::Cropping(DrawingArea* drawingAreaUse,QDockWidget* dockInfoToolP) : ToolEffect(drawingAreaUse){
    dockInfoTool = dockInfoToolP;
    isDrawingSelection = true;
    isDeselectionned = true;
}

//************************************************************* Affichage of Dock Tool Info

void Cropping::createAffichage(){
    QWidget* widgetInfoToolUp = new QWidget();
    QWidget* widgetInfoTool = new QWidget(widgetInfoToolUp);
    QGridLayout* layoutInfoTool = new QGridLayout();

    QLabel *labelInfoGenTool = new QLabel();

    labelInfoGenTool->setText(QString::fromUtf8("Tronquer l'image : selectionnez la zone puis clic droit dessus pour tronquer l'image"));

    layoutInfoTool->addWidget(labelInfoGenTool, 0, 0, Qt::AlignVCenter);

    widgetInfoTool->setLayout(layoutInfoTool);
    dockInfoTool->setWidget(widgetInfoToolUp);
}

//************************************************************* Cropping movement

void Cropping::beginSelectionRect(int x, int y){
    // if there are already a selection and if the user click on the inside of
    // it then, we crop the image (drawing area)
    if(!isDeselectionned && x > xBegin && y > yBegin && x < xEnd && y < yEnd){
        crop();
        isDeselectionned = true;
        return;
    }

    xBegin = x;
    yBegin = y;
    xEnd = x;
    yEnd = y;
    isDrawingSelection = true;
    isDeselectionned = false;
}

void Cropping::selectionningRect(int x, int y){
    xEnd = x;
    yEnd = y;
}

void Cropping::endSelectionRect(int x, int y){
    // If the user realease the crop on the same point that the one is press thenwe deselectionne
    if(isDeselectionned || (x == xBegin && y == yBegin)){
        isDeselectionned = true;
        return;
    }
    if(x < xBegin){
        xEnd = xBegin;
        xBegin = x;
    }else{
        xEnd = x;
    }
    if(y < yBegin){
        yEnd = yBegin;
        yBegin = y;
    }
    isDrawingSelection = false;
}

//************************************************************* Crop

void Cropping::crop(){
    list <Layer*> listLayers = drawingArea->getLayers();
    while(!listLayers.empty()){
        Layer* tmpLayer = listLayers.back();
        listLayers.pop_back();

        // newPos : position in reference with the drawing area
        int newPosX = max((int) tmpLayer->x(), xBegin);
        int newPosY = max((int) tmpLayer->y(), yBegin);
        int newPosXEnd = min((int) tmpLayer->x() + tmpLayer->getWidth(), xEnd);
        int newPosYEnd = min((int) tmpLayer->y() + tmpLayer->getHeight(), yEnd);

        // If the layer is out of the new drawing area, we erase it
        if(newPosXEnd < newPosX || newPosYEnd < newPosY){
            drawingArea->removeLayer(tmpLayer);
        }else{
            QImage imgCrop = tmpLayer->getImage()->copy(newPosX - tmpLayer->x(), newPosY - tmpLayer->y(), newPosXEnd - newPosX,newPosYEnd - newPosY);
            tmpLayer->setImage(new QImage(imgCrop));
            tmpLayer->setPos(newPosX - xBegin, newPosY - yBegin);
            // New limits of a layer
            tmpLayer->setSizeBoundingRect(xEnd - xBegin, yEnd - yBegin);
        }
    }
    drawingArea->resize(xEnd - xBegin, yEnd - yBegin);
    drawingArea->update();
    drawingArea->updateProprietyLayer();
}

void Cropping::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

    if(isDeselectionned){
        return;
    }

    if(isDrawingSelection){
        painter->drawRect(xBegin, yBegin, xEnd - xBegin, yEnd - yBegin);
    }else{
        painter->setPen(QColor(225, 225, 225));
        painter->drawRect(xBegin, yBegin, xEnd - xBegin, yEnd - yBegin);
        // top rectangle
        painter->fillRect(0, 0, drawingArea->getWidth(),yBegin, QColor(20, 20, 20, 80));
        // down rectangle
        painter->fillRect(0, yEnd, drawingArea->getWidth(), drawingArea->getHeight() - yEnd, QColor(20, 20, 20, 80));
        // right rectangle
        painter->fillRect(xEnd, yBegin, drawingArea->getWidth() - xEnd, yEnd - yBegin, QColor(20, 20, 20, 80));
        // left rectangle
        painter->fillRect(0, yBegin, xBegin, yEnd - yBegin, QColor(20, 20, 20, 80));
    }
}

//************************************************************* Get

int Cropping::getBeginX(){
    return xBegin;
}

int Cropping::getBeginY(){
    return yBegin;
}

int Cropping::getEndX(){
    return xEnd;
}

int Cropping::getEndY(){
    return yEnd;
}
